<template >
  <div style="padding: 20px" class="c">
    <el-form :model="form" label-width="120">
      <h3 class="h">登录界面</h3>
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    login() {
      this.$http.post('api/permission/getMenu',this.form).then(res =>{
        res = res.data
        if(res.code ===20000){
          //避免用户二次登录
          this.$store.commit('clearMenu')
          //登录成功
          this.$store.commit('setMenu',res.data.menu)
          //登录成功后将setToken引进来
          this.$store.commit('setToken',res.data)
          this.$store.commit('addMenu',this.$router)
          this.$router.push({name: 'home'})
        }else{
          this.$message.warning(res.data.message)
        }
      })
      // getMenu(this.form)
      //   .then(res => {
      //     if(res.data.code === -999) {
      //       this.$message.error(res.data.data.message)
      //       return
      //     } else {
      //       this.$message({
      //         message: '登录成功',
      //         type: 'success'
      //       });
      //       // 1. 存储 token
      //   localStorage.setItem('token', 'Bearer xxxx')
      //       this.$router.push('/main/home')
      //     }
      //   }, err => {
      //      // 登录失败
      //     localStorage.removeItem('token')
      //     console.log(err);
      //   })
      console.log('登录了');
    }
  }
}
</script>

<style lang="scss" scoped>
.c{
  height: 100%;
  background-color: aliceblue;
}
.el-form {
  width: 50%;
  margin: auto;
  padding: 50px;
  height: 450px;
  text-align: center;
  background-image: url(./2.png)
 
}
</style>
